/**
 * Created with IntelliJ IDEA.
 * Description: 用链表实现队列
 */
public class MyLinkedQueue {
    public static class ListNode{
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    ListNode head = new ListNode(0);
    private int usedSize;
    private ListNode front = head.next;
    private ListNode rear = head.next;

    //用链表作为队列的元素，不再存在队列满的情况，只有堆满的情况
    //入队
    public boolean enQueue(int value) {
        ListNode Node = new ListNode(value);
        if(isEmpty()) {
            head.next = Node;
            front = Node;
            rear = Node;
        }else {
            rear.next = Node;
            rear = rear.next;
        }
        usedSize++;
        return true;
    }

    //出队，删除队首元素
    public boolean deQueue() {
        if(isEmpty()) {
            return false;
        }
        if(front == rear) {
            head.next = null;
            front = null;
            rear = null;
        }else {
            head.next = front.next;
            front = head.next;
        }
        usedSize--;
        return true;
    }

    //返回队首元素
    public ListNode Front() {
        if(isEmpty()) {
            return null;
        }
        return front;
    }

    //返回队尾元素
    public ListNode Rear() {
        if(isEmpty()) {
            return null;
        }
        return rear;
    }

    public boolean isEmpty() {
        return usedSize == 0;
    }
}
